/*
 * @Descripttion   : 
 * @version        : 
 * @Author         : 
 * @Date           : 2025-07-01 14:05
 * @LastEditTime   : 2025-08-08 16:36
 */
/*
 * @Descripttion   : 
 * @version        : 
 * @Author         : 
 * @Date           : 2025-07-01 09:07
 * @LastEditTime   : 2025-07-01 14:04
 */
import Overlay from './Overlay'
export default class BoxEffect2 extends Overlay {
  constructor({
    sandbox,
    position = { x: 0, y: 0, z: 0 },
    rotation = { x: 0, y: 0, z: 0 },
    scale = 1,
    color = 0x00ffff, //0x00ffff #FF7C02 #e91010 #24ffcb
    alpha = 1.0,
    speed = 1.0,
    layers = 3,
    width = 0.5
  }) {
    super()
    this.sandbox = sandbox
    this.scene = sandbox.scene
    this._position = position
    this._rotation = rotation
    this._scale = scale
    this.color = color
    this.alpha = alpha
    this.speed = speed
    this.layers = layers
    this.width = 1 / width
    this.animationId = null;
    return this.initOverlay()
  }

  async initOverlay() {
    const mVertexShader = `
    varying vec2 vUv;
    varying float vIsSideFace;
    
    void main() {
        vUv = uv;
        vIsSideFace = step(0.5, 1.0 - abs(normal.y));             
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
    `
    const mFragmentShader = `
    uniform float uTime;
    uniform vec4 input_color;
    varying vec2 vUv;
    varying float vIsSideFace;
    #define PI 3.14159265
    void main(){
      if (vIsSideFace < 0.5) {
        discard;
    }   
    vec4 baseColor = input_color;

    vec4 finalColor;
        
    float amplitude = 1.;
    float frequency = 10.;
    
    float x = vUv.x;

    float y = sin(x * frequency) ;
    float t = 0.01*(-uTime*130.0);
    y += sin(x*frequency*2.1 + t)*4.5;
    y += sin(x*frequency*1.72 + t*1.121)*4.0;
    y += sin(x*frequency*2.221 + t*0.437)*5.0;
    y += sin(x*frequency*3.1122+ t*4.269)*2.5;
    y *= amplitude*0.06;
    y /= 3.;
    y += 0.55;

    vec4 color = gl_FragColor.rgba;

    float r = step(0.5, fract(vUv.y - uTime));

    baseColor.a = step(vUv.y,y) * (y-vUv.y)/y;
    
    gl_FragColor = baseColor;

    }
    `
    const material = new GC.ShaderMaterial({
      uniforms: {
        uTime: { value: 0 },
        input_color: {
          value: new GC.Vector4(
            new GC.Color(this.color).r,
            new GC.Color(this.color).g,
            new GC.Color(this.color).b,
            this.alpha
          )
        },
      },
      vertexShader: mVertexShader,
      fragmentShader: mFragmentShader,
      side: GC.DoubleSide,
      transparent: true,
      depthWrite: false,  // 关键设置！禁止写入深度缓冲区
      depthTest: false     // 保持深度测试启用
    });
    // material.renderOrder = 1;

    const updateMaterial = () => {
      material.uniforms.uTime.value += 0.01;
      this.animationId = requestAnimationFrame(updateMaterial);
    }

    // 启动动画
    updateMaterial();

    //const scaleArr = Array.isArray(this._scale) ? this._scale : [this._scale, this._scale, this._scale]
    const scaleArr= [this._scale.x,this._scale.y,this._scale.z]
    const geometry = new GC.BoxGeometry(1,1,1);
    this.overlay = new GC.Mesh(geometry, material);
    const { x, y, z } = this._position
    this.overlay.position.set(x, y, z)
    this.overlay.rotation.set(
      GC.MathUtils.degToRad(this._rotation.x), // 将度转换为弧度
      GC.MathUtils.degToRad(this._rotation.y),
      GC.MathUtils.degToRad(this._rotation.z)
    );
    this.overlay.scale.set(...scaleArr)
    this.scene.add(this.overlay)
    return this
  }

  // 停止动画
  stopAnimation() {
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
      this.animationId = null;
    }
  }
  zoomTo() {
    const box = new GC.Box3().setFromObject(this.overlay)
    const size = box.getSize(new GC.Vector3())
    const center = box.getCenter(new GC.Vector3())

    // 设置相机位置和朝向
    const maxSize = Math.max(size.x, size.y, size.z)
    const cameraDistance = maxSize * 2.0
    this.sandbox.camera.position.set(
      center.x + cameraDistance,
      center.y + cameraDistance / 2,
      center.z + cameraDistance / 2
    )
    this.sandbox.camera.lookAt(center.x, center.y, center.z)
  }

  get modelName() {
    const filePath = this.url
    const lastSlashIndex = filePath.lastIndexOf('/');
    const fileNameWithExt = filePath.substring(lastSlashIndex + 1);

    const lastDotIndex = fileNameWithExt.lastIndexOf('.');
    return fileNameWithExt.substring(0, lastDotIndex);
  }


}
